﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using Museum.DBAccess;
using System.Collections.Generic;

public partial class ManageVouchers : System.Web.UI.Page
{
    int gameId = 0;
    int voucherId = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        BindGridData();
    }
    /// <summary>
    /// Delete records from database
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void DeleteRecord(object sender, GridViewDeleteEventArgs e)
    {
        voucherId = Convert.ToInt32(GridViewVouchers.DataKeys[e.RowIndex].Value);
        int result = 0;
        try
        {
            result = DBAccess.DeleteVoucherData(voucherId);
            Alert.ShowMessage("Record Deleted successfully.");
            // Refresh the data
            BindGridData();
        }
        catch (Exception ex)
        {
            lblMessage.Text = ex.Message;
        }
        finally
        {
        }
    }

    /// <summary>
    /// Binds my grid data.
    /// </summary>
    private void BindGridData()
    {
        DataTable dtVouchers = new DataTable();
        DataTable dtGames = null;
        int museumId = 0;
        try
        {
            if (Session["MuseumId"] != null)
            {
                museumId = Convert.ToInt32(Session["MuseumId"]);
                dtGames = DBAccess.GetGameByMuseum(museumId);

                if (ViewState["IsGameBound"] == null)
                {
                    ddlGame.DataSource = dtGames;
                    ddlGame.DataBind();

                    ViewState["IsGameBound"] = "1";
                }
            }

            dtVouchers = DBAccess.GetVoucherDetails(Convert.ToInt32(ddlGame.SelectedValue));

            if (dtVouchers.Rows.Count > 0)
            {
                //assign DataTable to GridView
                GridViewVouchers.DataSource = dtVouchers;
                ViewState["gvData"] = dtVouchers;
                GridViewVouchers.DataBind();
            }
            else
            {
                GridViewVouchers.DataSource = new List<object>();
                GridViewVouchers.DataBind();
            }
        }
        catch (Exception ex)
        {
            lblMessage.Text = ex.Message;
        }
        finally
        {
        }
    }

    protected void ddlGame_SelectedIndexChanged1(object sender, EventArgs e)
    {
        BindGridData();
    }

    private string ConvertSortDirectionToSql(SortDirection sortDirection)
    {
        string newSortDirection = String.Empty;

        switch (sortDirection)
        {
            case SortDirection.Ascending:
                newSortDirection = "ASC";
                break;

            case SortDirection.Descending:
                newSortDirection = "DESC";
                break;
        }

        return newSortDirection;
    }

    protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridViewVouchers.PageIndex = e.NewPageIndex;
        GridViewVouchers.DataBind();
    }

    protected void gridView_Sorting(object sender, GridViewSortEventArgs e)
    {
        DataTable dataTable = GridViewVouchers.DataSource as DataTable;

        if (dataTable != null)
        {
            DataView dataView = new DataView(dataTable);
            dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);

            GridViewVouchers.DataSource = dataView;
            GridViewVouchers.DataBind();
        }
    }
}
